package cn.eng.product.controller;

import cn.eng.basic.exception.BusinessException;
import cn.eng.order.service.IOrderProductService;
import cn.eng.product.service.IProductService;
import cn.eng.product.domain.Product;
import cn.eng.product.query.ProductQuery;
import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    public IProductService productService;
    @Autowired
    private IOrderProductService orderProductService;

    /**
     * 接口：添加或修改
     * @param product  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.save(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                productService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Product loadById(@PathVariable("id")Long id) {
        return productService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Product> findAll(){
        return productService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query) {
        return productService.queryPage(query);
    }
    /**
     * 接口-上架
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids, HttpServletRequest request){
        try {
            productService.onsale(ids,request);
            return new AjaxResult();
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上架失败");
        }
    }

    /**
     * 接口-下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            productService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"下架失败");
        }
    }

    /**
     * 下单
     * @param product
     * @return
     */
    @PostMapping("/adopt")
    public AjaxResult adopt(@RequestBody Product product, HttpServletRequest request){
        try {
            return orderProductService.adopt(product,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"下单失败");
        }
    }
}
